const loginCode = document.querySelector('.login-body-code')
const toWin = loginCode.querySelector('img:nth-of-type(1)')
const loginForm = document.querySelector('.login-body-form')
const toCode = loginForm.querySelector('img:nth-of-type(1)')
const userInp = loginForm.querySelector('.user')
const pswInp = loginForm.querySelector('.psw')
const btn = loginForm.querySelector('.button')

//给toWin绑定点击事件
toWin.addEventListener('click', () => {
  loginCode.classList.add('active')  
})
//给toCode绑定点击事件
toCode.addEventListener('click', () => {
  loginCode.classList.remove('active')
})

//登录验证
 
  loginForm.addEventListener('click',(e) => {
    e = e || window.event
    const target = e.target || e.srcElement
    const user = userInp.value.trim()
    const psw = pswInp.value
    if(target.className === 'button'){
      const xhr = new XMLHttpRequest()
      xhr.open('GET',`http://localhost:8080/login?user=${user}&psw=${psw}`)
      xhr.onload = () => {
      const res = JSON.parse(xhr.responseText)
      if(res.code == 1){
        window.location.href = './index.html'
      }
      else{
        window.location.href = './regist.html'
      }
      console.log(res);
      }
      xhr.send()
    }
  })